package com.dyit.springcloud.feign.hystrix;

import com.dyit.springcloud.common.dto.HttpResp;
import com.dyit.springcloud.common.dto.RespCode;
import com.dyit.springcloud.feign.api.FeignApi;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 服务熔断的降级处理工厂
 */

@Slf4j
@Component
public class MyFallbackFactory implements FallbackFactory<FeignApi> {

    @Override
    public FeignApi create(Throwable throwable) {
        return new FeignApi() {
            @Override
            public HttpResp feignFindAll() {
                log.debug("出现异常，启动熔断");
                return new HttpResp.Builder()
                        .code(RespCode.FALLBACK_ERROE.getCode())
                        .msg(RespCode.FALLBACK_ERROE.getMsg())
                        .results(null)
                        .time(LocalDateTime.now())
                        .builder();
            }
        };
    }
}
